//
//  LaunchViewController.swift
//  cloudShop
//
//  Created by 孙琦 on 2019/1/3.
//  Copyright © 2019 京贝尔. All rights reserved.
//

import UIKit
import KooberUIKit
import KooberKit
import RxCocoa
import RxSwift

public class LaunchViewController: NiblessViewController {
  
  // MARK: - Properties
  let viewModel: LaunchViewModel
  let disposeBag = DisposeBag()

  // MARK: - Methods
  init(launchViewModelFactory: LaunchViewModelFactory) {
    self.viewModel = launchViewModelFactory.makeLaunchViewModel()
    super.init()
  }

  public override func loadView() {
    view = LaunchRootView(viewModel: viewModel)
  }

  public override func viewDidLoad() {
    super.viewDidLoad()
    observeErrorMessages()
  }

  func observeErrorMessages() {
    viewModel
      .errorMessages
      .asDriver { _ in fatalError("Unexpected error from error messages observable.") }
      .drive(onNext: { [weak self] errorMessage in
        guard let strongSelf = self else {
          return
        }
        strongSelf.present(errorMessage: errorMessage,
                           withPresentationState: strongSelf.viewModel.errorPresentation)
      })
      .disposed(by: disposeBag)
  }
}

protocol LaunchViewModelFactory {
  
  func makeLaunchViewModel() -> LaunchViewModel
}
